package com.quzhi.common;

import com.quzhi.common.code.Code;
import com.quzhi.sys.dto.InventoryDto;
import com.quzhi.sys.dto.ProductDto;
import com.quzhi.sys.entity.*;
import org.springframework.beans.BeanUtils;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class Utils {



    public static LocalDateTime convertToLocalDateTime(String dateTimeString) {
        // Define the formatter for the input string
        DateTimeFormatter formatter = DateTimeFormatter.ISO_DATE_TIME;

        // Parse the string into a ZonedDateTime (handles the 'Z' timezone)
        ZonedDateTime zonedDateTime = ZonedDateTime.parse(dateTimeString, formatter);

        // Convert ZonedDateTime to LocalDateTime (drops timezone information)
        return zonedDateTime.toLocalDateTime();
    }

    /**
     * 将 yyyy-MM-dd HH:mm:ss 格式的字符串转换为 LocalDateTime
     *
     * @param dateTimeString 日期时间字符串，格式为 yyyy-MM-dd HH:mm:ss
     * @return 转换后的 LocalDateTime 对象
     */
    public static LocalDateTime convertToLocalDateTimeToDate(String dateTimeString) {
        // 定义输入字符串的格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        // 使用格式化器解析字符串
        return LocalDateTime.parse(dateTimeString, formatter);
    }

    // 得到随机字符
    public static String randomStr(int n) {
        String str1 = "ABCDEFGHIJKLMNPQRSTUVWXYZabcdefghijklmnpqrstuvwxyz123456789";
        String str2 = "";
        int len = str1.length() - 1;
        double r;
        for (int i = 0; i < n; i++) {
            r = (Math.random()) * len;
            str2 = str2 + str1.charAt((int) r);
        }
        return str2;
    }


    // 生成一个10位数数字的随机字符串
    public static String generateRandomNumberString(String Str) {

        long timestamp = System.currentTimeMillis();


        return Str + timestamp; // 返回生成的随机数字字符串
    }


    public static OrderSerialNumber importOrderSerialNumber(ExportOrderAndSerialNumber SerialNumber, String Number) {
        OrderSerialNumber orderSerialNumber = new OrderSerialNumber();

        orderSerialNumber.setId(UUID.randomUUID().toString());
        orderSerialNumber.setOrderId(SerialNumber.getId());

        orderSerialNumber.setSerialNumber(Number);
        orderSerialNumber.setStatus(Code.DAI_CHU_KU);

        return orderSerialNumber;
    }


    /**
     * 将序列号添加到对应的商品中
     *
     * @param records       商品集合
     * @param inventoryList 序列号集合
     * @return 返回商品Dto
     */
    public static List<ProductDto> getProductDto(List<Product> records, List<Inventory> inventoryList) {
        List<ProductDto> productDtos = new ArrayList<>();
        for (Product record : records) {
            ProductDto productDto = new ProductDto();
            BeanUtils.copyProperties(record, productDto);

            // 存储所有序列号
            List<InventoryDto> inventories = new ArrayList<>();
            for (Inventory inventory : inventoryList) {
                if (inventory.getProductId().equals(record.getId())) {
                    InventoryDto inventoryDto = new InventoryDto();
                    BeanUtils.copyProperties(inventory, inventoryDto);

                    inventories.add(inventoryDto);
                }
            }

            productDto.setInventoryDto(inventories);
            productDtos.add(productDto);
        }

        return productDtos;
    }

    // 解析收货信息
    public static List<String> goodsInfo(String str){
        List<String> data = new ArrayList<>();

        data.add("缺配件");
        data.add("损坏");

        List<String> info = new ArrayList<>();

        if(str == null) return info;

        for (String datum : data) {
            if(str.contains(datum)) info.add(datum);

        }
        return info;
    }


    public static String removeLastSlash(String str) {
        int lastSlashIndex = str.lastIndexOf('/');
        if (lastSlashIndex == -1) {
            return str; // 如果没有找到斜杠，返回原字符串
        }
        return str.substring(0, lastSlashIndex) + str.substring(lastSlashIndex + 1);
    }


    /**
     * 获取Token信息
     */
    public static String getToken(HttpServletRequest request){
        return request.getHeader("qz-Token");
    }

    /**
     * 分页查询
     * @param list
     * @param pageNumber
     * @param pageSize
     * @return
     * @param <T>
     */
    public static <T> List<T> paginate(List<T> list, long pageNumber, long pageSize) {
        return IntStream.range(0, list.size())
                .skip((long) (pageNumber - 1) * pageSize)
                .limit(pageSize)
                .mapToObj(list::get)
                .collect(Collectors.toList());
    }
}
